package com.yml.mall.product;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.yml.mall.enums.ProductStatus;
import com.yml.mall.enums.ResponseEnum;
import com.yml.mall.product.dao.ProductMapper;
import com.yml.mall.product.pojo.ProductDO;
import com.yml.mall.product.pojo.ProductDetailDOT;
import com.yml.mall.product.pojo.ProductListDTO;
import com.yml.mall.vo.PageDTO;
import com.yml.mall.vo.ResponseVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.yml.mall.consts.myConst.ROOT_CATEGORY_ID;

/**
 * @ClassName ProductServiceImpl
 * @Author Administrator
 * @Date 2020/4/10   9:36
 */
@Service
public class ProductServiceImpl implements  ProductService {

    private final ProductMapper productMapper;

    private ProductServiceImpl(ProductMapper productMapper){
        this.productMapper=productMapper;
    }

    @Override
    public ResponseVO<PageDTO<ProductListDTO>> findProductListFromAll(Integer page, Integer size) {

        PageHelper.startPage(page,size);
        List<ProductListDTO> productListDTOS=ProductDOs2ProductListDTOs(productMapper.selectAll());
        int total=productMapper.countAll();
       PageDTO<ProductListDTO> pageDTO =new PageDTO(page,size,PageHelper.count(new ISelect() {
           @Override
           public void doSelect() {
               productMapper.selectAll();
           }
       }));
       pageDTO.setList(productListDTOS);
        return ResponseVO.success(pageDTO);

    }

    @Override
    public ResponseVO<ProductDetailDOT> detail(Integer id) {
        ProductDO productDO=productMapper.selectByPrimaryKey(id);
        //产品不存在
        if(productDO==null ) ResponseVO.error(ResponseEnum.SALE_NOT_EXIST);
        //商品下架或者删除
        if((productDO.getStatus()== ProductStatus.SALE_OFF.getCode()||
                productDO.getStatus()==ProductStatus.DELETE.getCode()))
            return ResponseVO.error(ResponseEnum.SALE_OFF_OR_DELETE);

        ProductDetailDOT productDetailDOT=new ProductDetailDOT();
        BeanUtils.copyProperties(productDO,productDetailDOT);
        //隐藏信息
        productDetailDOT.setStock(productDetailDOT.getStock()>100?100:productDetailDOT.getStock());

        return ResponseVO.success(productDetailDOT);
    }

    @Override
    public ResponseVO<PageDTO<ProductListDTO>> findProductListFromParent(Integer categoryId, Integer page, Integer size) {
        if(categoryId==ROOT_CATEGORY_ID) {
            return findProductListFromAll(page,size);
        }
        PageHelper.startPage(page,size);
        List<ProductDO> productDOS =productMapper.selectByCategory(categoryId);

        List<ProductListDTO> productListDTOS=ProductDOs2ProductListDTOs(productDOS);
        PageDTO<ProductListDTO> productListDTOPageDTO=new PageDTO<>(page,size,PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                productMapper.selectByCategory(categoryId);
            }
        }),productListDTOS);
        return ResponseVO.success(productListDTOPageDTO);
    }



    //List<ProductDO> 转换成 List<ProductListDTO>
    private List<ProductListDTO> ProductDOs2ProductListDTOs(List<ProductDO> productDOS){
        List<ProductListDTO> productListDTOS=new ArrayList<>();
        for(int i=0;i<productDOS.size();i++){
            ProductListDTO productListDTO=new ProductListDTO();
            BeanUtils.copyProperties(productDOS.get(i),productListDTO);
            productListDTOS.add(productListDTO);
        }
        return productListDTOS;
    }
}

